package com.laikan.reader.core.adapter;

import android.content.Context;
import android.net.Uri;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.*;
import com.laikan.library.utils.DeviceUtil;
import com.laikan.reader.R;
import com.laikan.reader.core.bean.BookFocusBean;

import java.util.ArrayList;

/**
 * Created by Administrator on 2016/10/19.
 */
public class CollectAdapter extends BaseAdapter {

    public static final int TYPE_CONTENT = 1; //  内容与图片类型
    public static final int TYPE_TEXT = 0; // 文本类型
    public static final int TYPE_IMG = 2; //图片类型

    private Context mContext;
    private LayoutInflater layoutInflater;
    private ArrayList<BookFocusBean.BookFocusDataList> collectDatas;
    public boolean selectedIsShow = false; //默认单选按钮不显示

    public CollectAdapter(Context context, ArrayList<BookFocusBean.BookFocusDataList> datas) {
        this.mContext = context;
        this.collectDatas = datas;
        layoutInflater = LayoutInflater.from(mContext);
    }

    // 设置单选按钮是否显示并更新列表
    public void updateSelectorShow(boolean selectorShow) {
        this.selectedIsShow = selectorShow;
        notifyDataSetChanged();
    }

    //添加数据
    public void addDatas(ArrayList<BookFocusBean.BookFocusDataList> datas) {
        collectDatas.addAll(datas);
        notifyDataSetChanged();
    }

    //获取数据
    public ArrayList<BookFocusBean.BookFocusDataList> getDatas() {
        return this.collectDatas;
    }

    //更新数据
    public void updateDatas(ArrayList<BookFocusBean.BookFocusDataList> datas) {
        this.collectDatas = datas;
        this.notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return collectDatas.size();
    }

    @Override
    public Object getItem(int i) {
        return collectDatas.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public int getItemViewType(int position) {
        if (Integer.parseInt(collectDatas.get(position).getType()) == 1) {
            return TYPE_TEXT;
        }else if (Integer.parseInt(collectDatas.get(position).getType()) == 2) {
            return TYPE_CONTENT;
        }
        return TYPE_IMG;
    }

    @Override
    public int getViewTypeCount() {
        return 3;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup arg2) {
        ViewHolderContent viewHolderContent = null;
        ViewHolderText viewHolderText = null;
        ViewHolderImg viewHolderImg = null;
        BookFocusBean.BookFocusDataList collectBean = null;
        int type = 0;
        if (position <= collectDatas.size() - 1) {
            type = getItemViewType(position);
            collectBean = collectDatas.get(position);
        }
        if (convertView == null) {
            switch (type) {
                case TYPE_CONTENT:
                    convertView = layoutInflater.inflate(R.layout.lk_collect_content_item, null);
                    viewHolderContent = new ViewHolderContent();
                    viewHolderContent.content_collect_selector = (CheckBox)convertView.findViewById(R.id.content_collect_selector);
                    viewHolderContent.content_collect_title = (TextView)convertView.findViewById(R.id.content_collect_title);
                    viewHolderContent.content_collect_readings = (TextView)convertView.findViewById(R.id.content_collect_readings);
                    viewHolderContent.content_collect_author = (TextView)convertView.findViewById(R.id.content_collect_author);
                    viewHolderContent.content_collect_img = (ImageView)convertView.findViewById(R.id.content_collect_img);
                    convertView.setTag(viewHolderContent);
                    break;
                case TYPE_TEXT:
                    convertView = layoutInflater.inflate(R.layout.lk_collect_text_item, null);
                    viewHolderText = new ViewHolderText();
                    viewHolderText.text_collect_selector = (CheckBox)convertView.findViewById(R.id.text_collect_selector);
                    viewHolderText.text_collect_title = (TextView)convertView.findViewById(R.id.text_collect_title);
                    viewHolderText.text_collect_content = (TextView)convertView.findViewById(R.id.text_collect_content);
                    viewHolderText.text_collect_author = (TextView)convertView.findViewById(R.id.text_collect_author);
                    viewHolderText.item_collect_readings = (TextView)convertView.findViewById(R.id.item_collect_readings);
                    convertView.setTag(viewHolderText);
                    break;
                case TYPE_IMG:
                    convertView = layoutInflater.inflate(R.layout.lk_collect_img_item, null);
                    viewHolderImg = new ViewHolderImg();
                    viewHolderImg.img_collect_selector = (CheckBox)convertView.findViewById(R.id.img_collect_selector);
                    viewHolderImg.img_collect_title = (TextView)convertView.findViewById(R.id.img_collect_title);
                    viewHolderImg.img_collect_author = (TextView)convertView.findViewById(R.id.img_collect_author);
                    viewHolderImg.img_collect_readings = (TextView)convertView.findViewById(R.id.img_collect_readings);
                    viewHolderImg.img_collect_img = (ImageView)convertView.findViewById(R.id.img_collect_img);
                    convertView.setTag(viewHolderImg);
                    break;
                default:
                    break;
            }
        }else {
            switch (type) {
                case TYPE_IMG:
                    viewHolderImg = (ViewHolderImg)convertView.getTag();
                    break;
                case TYPE_CONTENT:
                    viewHolderContent = (ViewHolderContent)convertView.getTag();
                    break;
                case TYPE_TEXT:
                    viewHolderText = (ViewHolderText)convertView.getTag();
                    break;
                default:
                    break;
            }
        }
        switch (type) {
            case TYPE_CONTENT:
                viewHolderContent.content_collect_title.setText(collectBean.getTitle());
                viewHolderContent.content_collect_readings.setText(DeviceUtil.getHtmlFormatedString(mContext, R.string.bookfocus_article_record, collectBean.getReadNum()));
                viewHolderContent.content_collect_author.setText(collectBean.getAuthor());
                if (!TextUtils.isEmpty(collectBean.getImg())) {
                    viewHolderContent.content_collect_img.setImageURI(Uri.parse(collectBean.getImg()));
                }
                if (selectedIsShow) {   // To determine whether a entry has been selected
                    viewHolderContent.content_collect_selector.setVisibility(View.VISIBLE);
                    if (collectBean.isSelected()) {
                        viewHolderContent.content_collect_selector.setChecked(true); // set show already selected
                    }else {
                        viewHolderContent.content_collect_selector.setChecked(false); // set show not selected
                    }
                }else {
                    viewHolderContent.content_collect_selector.setVisibility(View.GONE);
                }
                break;
            case TYPE_TEXT:
                viewHolderText.text_collect_title.setText(collectBean.getTitle());
                viewHolderText.text_collect_content.setText(collectBean.getIntro());
                viewHolderText.text_collect_author.setText(collectBean.getAuthor());
                viewHolderText.item_collect_readings.setText(DeviceUtil.getHtmlFormatedString(mContext, R.string.bookfocus_article_record, collectBean.getReadNum()));
                if (selectedIsShow) {
                    viewHolderText.text_collect_selector.setVisibility(View.VISIBLE);
                    if (collectBean.isSelected()) {
                        viewHolderText.text_collect_selector.setChecked(true);
                    }else {
                        viewHolderText.text_collect_selector.setChecked(false);
                    }
                }else {
                    viewHolderText.text_collect_selector.setVisibility(View.GONE);
                }
                break;
            case TYPE_IMG:
                viewHolderImg.img_collect_title.setText(collectBean.getTitle());
                viewHolderImg.img_collect_readings.setText(DeviceUtil.getHtmlFormatedString(mContext, R.string.bookfocus_article_record, collectBean.getReadNum()));
                viewHolderImg.img_collect_author.setText(collectBean.getAuthor());
                if (!TextUtils.isEmpty(collectBean.getImg())) {
                    viewHolderImg.img_collect_img.setImageURI(Uri.parse(collectBean.getImg()));
                }
                if (selectedIsShow) {
                    viewHolderImg.img_collect_selector.setVisibility(View.VISIBLE);
                    if (collectBean.isSelected()) {
                        viewHolderImg.img_collect_selector.setChecked(true);
                    }else {
                        viewHolderImg.img_collect_selector.setChecked(false);
                    }
                }else {
                    viewHolderImg.img_collect_selector.setVisibility(View.GONE);
                }
                break;
        }
        return convertView;
    }

    class ViewHolderText {

        TextView text_collect_title, text_collect_content, text_collect_author, item_collect_readings;
        CheckBox text_collect_selector;
    }

    class ViewHolderContent {

        TextView content_collect_title, content_collect_author, content_collect_readings;
        ImageView content_collect_img;
        CheckBox content_collect_selector;
    }

    class ViewHolderImg {

        TextView img_collect_title, img_collect_author, img_collect_readings;
        ImageView img_collect_img;
        CheckBox img_collect_selector;
    }
}
